package main

import(
	"fmt"
)

func main(){
	fmt.Println(lengthOfLongestSubstring("abcabcbb"))
	fmt.Println(lengthOfLongestSubstring("cc"))
}

func lengthOfLongestSubstring(s string) int {
	indexMap := make(map[byte]int)
	maxLen := 0
	m := 0
	for i := 0; i < len(s); i++ {
		if index, ok := indexMap[s[i]]; ok {
			m = max(m, index + 1)
		}
		indexMap[s[i]] = i
		maxLen = max(maxLen, i - m + 1)
	}
    return maxLen
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}